import fs from "fs";
import { getOptions } from "./options";
import { main } from "./main";
import path from "path/posix";
import { handleContent } from "./handleContent";
import { transform } from "./req";

const { rootPath, outerPath, from, to, lazy, targets } = getOptions();

if (!rootPath || !outerPath || !from || !to) {
  console.log(`rootPath | outerPath | from | to 为必填项`);
} else {
  //测试接口是否能用
  transform("我", "zh", "en").then(({ data }) => {
    if (data.error_code) {
      console.log(
        `\n接口未接通,错误信息↓↓↓↓${
          data.error_code === "58000" ? "\t修改开发文档服务器地址即可" : ""
        }`
      );
      console.log(data);
    } else {
      //如果路径是一个文件则直接翻译文件
      if (lazy && targets?.length) {
        return main();
      }
      if (path.extname(rootPath)) {
        handleContent({ importPath: rootPath, outerPath })().then(
          (info: string) => {
            console.log(info);
          }
        );
      } else {
        //如果路径是一个文件夹则翻译这个文件夹下所有的国际化文件
        try {
          fs.readdirSync(outerPath);
          console.error(outerPath + " 文件夹已经存在");
        } catch (e) {
          fs.mkdirSync(outerPath);
          main();
        }
      }
    }
  });
}
